<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="110px" :rules="formRules">
                <el-form-item label="短剧编号" prop="drama_id">
                    <el-input v-model="formData.drama_id" disabled placeholder="请输入短剧编号" />
                </el-form-item>
                <el-form-item label="短剧标题" prop="title">
                    <el-input v-model="formData.title" placeholder="请输入短剧标题" />
                </el-form-item>
                <el-form-item label="封面图URL" prop="cover_url">
                    <material-picker v-model="formData.cover_url" :limit="1" />
                </el-form-item>
                <el-form-item label="详细描述" prop="description">
                    <el-input
                        v-model="formData.description"
                        placeholder="请输入详细描述"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                    />
                </el-form-item>
                <el-form-item label="总集数" prop="total_episodes">
                    <el-input v-model="formData.total_episodes" disabled placeholder="请输入总集数" />
                </el-form-item>
                <el-form-item label="价格" prop="money">
                    <el-input-number v-model="formData.money" :min="0" :max="10000" placeholder="请输入价格" />
                </el-form-item>
                <el-form-item label="是否完结" prop="is_completed">
                    <div>
                        <el-switch v-model="formData.is_completed" :active-value="1" :inactive-value="0" />
                    </div>
                </el-form-item>

                <el-form-item label="是否允许观看" prop="status">
                    <div>
                        <el-switch v-model="formData.status" :active-value="1" :inactive-value="0" />
                    </div>
                </el-form-item>

            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  dramaEdit, dramaAdd, dramaDetail } from '@/api/client/drama'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑短剧' : '新增短剧'
})

const formData = reactive({
    drama_id: '',
    title: '',
    description: '',
    cover_url: '',
    is_completed: 0,
    status: 0,
    total_episodes: '',
    heat_value: '',
    money: 0,
    tag: '',
})

const formRules = {
    drama_id: [
        {
            required: true,
            message: '请输入短剧唯一标识(ULID格式)',
            trigger: ['blur']
        }
    ],
    title: [
        {
            required: true,
            message: '请输入短剧标题',
            trigger: ['blur']
        }
    ],
    description: [
        {
            required: true,
            message: '请输入详细描述',
            trigger: ['blur']
        }
    ],
    cover_url: [
        {
            required: true,
            message: '请输入封面图URL',
            trigger: ['blur']
        }
    ],
    total_episodes: [
        {
            required: true,
            message: '请输入总集数',
            trigger: ['blur']
        }
    ],
    money: [
        {
            required: true,
            message: '请输入价格',
            trigger: ['blur']
        }
    ],
    is_completed: [
        {
            required: true,
            message: '请选择是否完结',
            trigger: ['blur']
        }
    ],
    status: [
        {
            required: true,
            message: '请输入允许观看',
            trigger: ['blur']
        }
    ],
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await dramaEdit(data) : await dramaAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    console.log("drama_id:",row.drama_id)
    const data = await dramaDetail({
        drama_id: row.drama_id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
